package com.wenjie.original.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.wenjie.common.utils.StringUtils;
import com.wenjie.common.utils.TokenUtil;
import com.wenjie.original.entity.SysCodeTemp;
import com.wenjie.original.entity.SysCodeTempGroup;
import com.wenjie.original.entity.SysCodeTempHistory;
import com.wenjie.original.entity.vo.SysCodeTempGroup.SysCodeTempGroupAddVo;
import com.wenjie.original.entity.vo.SysCodeTempGroup.SysCodeTempGroupSelVo;
import com.wenjie.original.entity.vo.SysCodeTempGroup.SysCodeTempGroupUpVo;
import com.wenjie.original.flex_config.baseInterface.Cus_ServiceImp;
import com.wenjie.original.mapper.SysCodeTempGroupMapper;
import com.wenjie.original.service.ISysCodeTempGroupService;
import com.wenjie.original.service.ISysCodeTempHistoryService;
import com.wenjie.original.service.ISysCodeTempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 代码组模版 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-25
 */
@Service
public class SysCodeTempGroupServiceImpl extends Cus_ServiceImp<SysCodeTempGroupMapper, SysCodeTempGroup> implements ISysCodeTempGroupService {

    @Autowired
    private ISysCodeTempService sysCodeTempService;

    @Autowired
    private ISysCodeTempHistoryService sysCodeTempHistoryService;


    @Override
    @Transactional
    public void saveTempGroup(SysCodeTempGroupAddVo addVo) {
        long sysCodeTempGroupId = IdUtil.getSnowflakeNextId();

        // 代码组模版保存
        SysCodeTempGroup sysCodeTempGroup = BeanUtil.toBean(addVo.getGroupInfo(), SysCodeTempGroup.class);
        sysCodeTempGroup.setSysCodeTempGroupId(sysCodeTempGroupId);
        sysCodeTempGroup.setCusParam(JSONUtil.parseObj(sysCodeTempGroup.getCusParam()).toStringPretty());
        sysCodeTempGroup.setSysAdminId(TokenUtil.getTokenInfo().getId());
        this.save(sysCodeTempGroup);

        // 代码模版保存
        List<SysCodeTemp> sysCodeTemps = new ArrayList<>();
        for (SysCodeTempGroupAddVo.TempList tempList : addVo.getTempList()) {
            SysCodeTemp sysCodeTemp = BeanUtil.toBean(tempList, SysCodeTemp.class);
            sysCodeTemp.setSysCodeTempGroupId(sysCodeTempGroupId);
            sysCodeTemps.add(sysCodeTemp);
        }
        sysCodeTempService.saveBatch(sysCodeTemps);


        // 代码模版历史记录保存
        SysCodeTemp sysCodeTemp = new SysCodeTemp();
        sysCodeTemp.setSysCodeTempGroupId(sysCodeTempGroupId);
        List<SysCodeTemp> list = sysCodeTempService.list(QueryWrapper.create(sysCodeTemp));

        List<SysCodeTempHistory> sysCodeTempHistories = new ArrayList<>();
        for (SysCodeTemp codeTemp : list) {
            SysCodeTempHistory sysCodeTempHistory = new SysCodeTempHistory();
            sysCodeTempHistory.setSysCodeTempGroupId(sysCodeTempGroupId);
            sysCodeTempHistory.setSysCodeTempId(codeTemp.getSysCodeTempId());
            sysCodeTempHistory.setCentent(JSONUtil.toJsonStr(codeTemp));
            sysCodeTempHistories.add(sysCodeTempHistory);
        }
        sysCodeTempHistoryService.saveBatch(sysCodeTempHistories);
    }

    @Override
    @Transactional
    public void update(SysCodeTempGroupUpVo upVo) {
        Long sysCodeTempGroupId = upVo.getGroupInfo().getSysCodeTempGroupId();


        // 修改代码组模版
        SysCodeTempGroupUpVo.GroupInfo groupInfo = upVo.getGroupInfo();
        String cusParam = groupInfo.getCusParam();
        if (StringUtils.isNotEmpty(cusParam))
            groupInfo.setCusParam(JSONUtil.parseObj(cusParam).toStringPretty()); // 将json格式化一下
        this.updateById(BeanUtil.toBean(groupInfo, SysCodeTempGroup.class));

        // 代码模版的添加和修改和删除
        // 删除一下没有的代码
        Set<Long> collect = sysCodeTempService.listByGroupId(sysCodeTempGroupId).stream().map(s -> s.getSysCodeTempId()).collect(Collectors.toSet());
        Set<Long> removeSet = new HashSet<>();
        upVo.getTempList().stream().forEach(s -> {
            if (collect.contains(s.getSysCodeTempId())) {
                removeSet.add(s.getSysCodeTempId());
            }
        });
        if (removeSet.size() > 0) sysCodeTempService.removeByIds(removeSet);


        // 创建或者修改模版
        List<SysCodeTemp> sysCodeTemps = new ArrayList<>();
        for (SysCodeTempGroupUpVo.TempList tempList : upVo.getTempList()) {
            SysCodeTemp sysCodeTemp = BeanUtil.toBean(tempList, SysCodeTemp.class);
            sysCodeTemp.setSysCodeTempId(null);
            sysCodeTemp.setSysCodeTempGroupId(sysCodeTempGroupId);
            sysCodeTemps.add(sysCodeTemp);
        }
        sysCodeTempService.saveOrUpdateBatch(sysCodeTemps);


        // 代码模版历史记录保存
        SysCodeTemp sysCodeTemp = new SysCodeTemp();
        sysCodeTemp.setSysCodeTempGroupId(sysCodeTempGroupId);
        List<SysCodeTemp> list = sysCodeTempService.list(QueryWrapper.create(sysCodeTemp));

        List<SysCodeTempHistory> sysCodeTempHistories = new ArrayList<>();
        for (SysCodeTemp codeTemp : list) {
            SysCodeTempHistory sysCodeTempHistory = new SysCodeTempHistory();
            sysCodeTempHistory.setSysCodeTempGroupId(sysCodeTempGroupId);
            sysCodeTempHistory.setSysCodeTempId(codeTemp.getSysCodeTempId());
            sysCodeTempHistory.setCentent(JSONUtil.toJsonStr(codeTemp));
            sysCodeTempHistories.add(sysCodeTempHistory);
        }
        sysCodeTempHistoryService.saveBatch(sysCodeTempHistories);


    }

    @Override
    public SysCodeTempGroupSelVo detailById(Long sysCodeTempGroupId) {

        // 查询代码组模版
        SysCodeTempGroupSelVo sysCodeTempGroupSelVo = new SysCodeTempGroupSelVo();
        SysCodeTempGroup sysCodeTempGroup = this.getById(sysCodeTempGroupId);
        sysCodeTempGroupSelVo.setGroupInfo(BeanUtil.toBean(sysCodeTempGroup, SysCodeTempGroupSelVo.GroupInfo.class));


        // 查询代码模版
        SysCodeTemp sysCodeTemp = new SysCodeTemp();
        sysCodeTemp.setSysCodeTempGroupId(sysCodeTempGroupId);
        List<SysCodeTemp> sysCodeTemps = sysCodeTempService.list(QueryWrapper.create(sysCodeTemp));

        List<SysCodeTempGroupSelVo.TempList> tempLists = new ArrayList<>();
        for (SysCodeTemp codeTemp : sysCodeTemps) {
            tempLists.add(BeanUtil.toBean(codeTemp, SysCodeTempGroupSelVo.TempList.class));
        }
        sysCodeTempGroupSelVo.setTempList(tempLists);

        return sysCodeTempGroupSelVo;
    }

    @Override
    @Transactional
    public void delById(Long sysCodeTempGroupId) {
        this.removeById(sysCodeTempGroupId);
        sysCodeTempService.removeByGroupId(sysCodeTempGroupId);
    }
}
